package com.easybuilder.common.utils;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {

    private static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
    private static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    private static final String FORMAT_HOUR = "yyyy-MM-dd HH";
    private static final String FORMAT_DAY = "yyyy-MM-dd";
    private static final String FORMAT_MONTH = "yyyy-MM";
    private static final String FORMAT_YEAR = "yyyy";

    public static String getFormatSecond(Date date) {
        return getTimeStr(date, FORMAT_SECOND);
    }

    public static String getFormatMinute(Date date) {
        return getTimeStr(date, FORMAT_MINUTE);
    }

    public static String getFormatHour(Date date) {
        return getTimeStr(date, FORMAT_HOUR);
    }

    public static String getFormatDay(Date date) {
        return getTimeStr(date, FORMAT_DAY);
    }

    public static String getFormatMonth(Date date) {
        return getTimeStr(date, FORMAT_MONTH);
    }

    public static String getFormatYear(Date date) {
        return getTimeStr(date, FORMAT_YEAR);
    }

    //获取自定义格式时间
    public static String getTimeStr(Date date, String format) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
        return null;
    }

    //字符串转日期
    public static Date getDate(String timeStr, String format) {
        if (StringUtil.isNotEmpty(timeStr) && StringUtil.isNotEmpty(format)) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                return sdf.parse(timeStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
